import requests #网络请求
import re #正则
import urllib # 下载

for n in range(1, 29):
    url = "http://www.ximalaya.com/youshengshu/11678383/p"+str(n)+"/"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
        'Accept-Language': 'zh-CN,zh;q=0.8'}
    html = requests.get(url, headers=headers)
    data = re.findall('<div class="e-2304105070 text"><a title=".*?" href="/youshengshu/11678383/(.*?)">.*?</a></div>',html.text)
    for m in set(data):
        urls = "http://www.ximalaya.com/revision/play/tracks?trackIds="+str(m)
        html2 = requests.get(urls, headers=headers)
        name = html2.json()['data']['tracksForAudioPlay'][0]['trackName']
        src = html2.json()['data']['tracksForAudioPlay'][0]['src']
        urllib.request.urlretrieve(src, 'E:\\book\\'+name+'.m4a')  # 提前要创建文件夹
        print(name+'下载完成')




